package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MenuUI extends JMenuBar implements ActionListener {


	private JMenu fichierMenu, outilsMenu, helpMenu, lookAndFeelMenu;
	private JMenuItem quitter, parametres, apropos;

	private ButtonGroup lookAndFeelGroup;

	private JFrame frame;
	
	private class LookAndFeelRadio extends JRadioButtonMenuItem {
		UIManager.LookAndFeelInfo info;
		
		public LookAndFeelRadio(UIManager.LookAndFeelInfo info) {
			super(info.getName());
			this.info = info;
		}

		public UIManager.LookAndFeelInfo getInfo() {
			return info;
		}
	}
	
	private MenuUI() {}
	
	public MenuUI(JFrame parentFrame) {
		frame = parentFrame;
		
		fichierMenu = new JMenu("Fichier");
		quitter = new JMenuItem("Quitter");
		quitter.addActionListener(this);
		fichierMenu.add(quitter);

		add(fichierMenu);
		
		outilsMenu = new JMenu("Outils");
		lookAndFeelMenu = new JMenu("Look-and-feel");
		
		lookAndFeelGroup = new ButtonGroup();
		
		for (UIManager.LookAndFeelInfo lookAndFeelInfo : UIManager.getInstalledLookAndFeels()) {
			LookAndFeelRadio lookAndFeelMenuItem = new LookAndFeelRadio(lookAndFeelInfo);
			if (UIManager.getLookAndFeel().getName() == lookAndFeelInfo.getName()) {
				/* Look-and-feel actuel - selectionner le radiobutton */
				lookAndFeelMenuItem.setSelected(true);
			}
			lookAndFeelMenuItem.setActionCommand(lookAndFeelInfo.getClassName());
			lookAndFeelMenuItem.addActionListener(this);
			lookAndFeelGroup.add(lookAndFeelMenuItem);
			lookAndFeelMenu.add(lookAndFeelMenuItem);
		}
		
		outilsMenu.add(lookAndFeelMenu);
		
		parametres = new JMenuItem("Paramètres");
		parametres.addActionListener(this);
		outilsMenu.add(parametres);
		add(outilsMenu);

		helpMenu = new JMenu("Aide");
		apropos = new JMenuItem("À propos");
		apropos.addActionListener(this);
		helpMenu.add(apropos);
		add(helpMenu);
	}
	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == quitter) {
			frame.dispose();

		} else if (e.getSource() == parametres) {
			ParametreUI param = new ParametreUI();
			param.setVisible(true);
		} else if (e.getSource() == apropos) {
			AproposUI apropos = new AproposUI();
			apropos.setVisible(true);

		} else if (e.getSource() instanceof LookAndFeelRadio) {
			try {
				UIManager.setLookAndFeel(((LookAndFeelRadio)e.getSource()).getInfo().getClassName());
				SwingUtilities.updateComponentTreeUI(frame);
				frame.pack();
				System.out.println("Look-and-feel changed.");
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InstantiationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (UnsupportedLookAndFeelException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
	}

}
